package com.goldplusgold.tradedelay.app.home.live.base;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.PorterDuff;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ProgressBar;

import com.goldplusgold.live.bean.BaseBean;
import com.goldplusgold.support.lib.R;
import com.goldplusgold.support.lib.base.activity.BaseActivity;
import com.goldplusgold.support.lib.base.fragment.BaseFragment;
import com.goldplusgold.support.lib.base.fragment.FragmentBasicDialog;
import com.goldplusgold.tradedelay.app.base.constant.ErrorCodeConstant;
import com.goldplusgold.tradedelay.app.base.utils.ActivityUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.lang.ref.WeakReference;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/8/7.
 */

public abstract class BaseCallback<T> implements Callback<T> {
    protected Dialog mDialog;

    private WeakReference<Context> context ;

    public BaseCallback(Context context){
        this.context = new WeakReference<>(context) ;
        if(this.context.get() != null){
            before();
        }
    }

    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        if(context == null) {
            complete();
            return;
        }
        Log.e("BaseCallBack" , "成功");
        Context c = context.get();
        if(c == null){
            complete();
            return;
        }
        if(c instanceof BaseActivity){
            if(((BaseActivity) c).isFinishing()){
                complete();
                return;
            }else{
                T t = response.body();
                if(t instanceof BaseBean){
                    if(TextUtils.equals( ((BaseBean)t).getErrorCode() , ErrorCodeConstant._EXTRUSION_SIGN_IN_CODE)){
                        String message = ((BaseBean)t).getErrorMsg() ;
                        ActivityUtils.isQuit((Activity)c, message);
                        complete();
                        return;
                    }
                }
                success(call , response);
            }
        }

        complete();
    }

    @Override
    public void onFailure(Call<T> call, Throwable t) {
        Context c = context.get();
        if(c == null){
            complete();
            return;
        }
        Log.d("BaseCallBack" , "失败");
        if(t.getMessage() == null){
            complete();
            return;
        }
        if(c instanceof BaseActivity){
            if(((BaseActivity) c).isFinishing()){
                complete();
                return;
            }else{
                if(t.getMessage().contains("closed")) {
                    //如果是主动取消的情况下
                    complete();
                    return;
                }else {
                    //其他情况下
                    faile(call , t);
                }
            }
        }
        complete();
    }

    public abstract void success(Call<T> call , Response<T> response);

    public abstract void faile(Call<T> call , Throwable t);

    public void before(){
        showLoadingControl();
    }

    public void complete(){
        dismisLoadingControl();
    }



    protected void showLoadingControl() {
        showLoadingControl(null, true);
    }

    protected void showLoadingControl(String msg, boolean isCancelable) {

        if (mDialog == null) {
            mDialog = new Dialog(context.get(), R.style.LoadingCustomDialog);
            mDialog.setContentView(R.layout.layout_dialog_loading);
            ((ProgressBar) mDialog.findViewById(R.id.id_progress_dialog_loading)).getIndeterminateDrawable().setColorFilter(context.get().getResources().getColor(R.color.orange), PorterDuff.Mode.SRC_IN);
            mDialog.setCanceledOnTouchOutside(isCancelable);
            mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    onDialogLoadingCancel();
                }
            });
        }
        mDialog.show();
    }

    protected void dismisLoadingControl() {
        dismissDialog();
    }

    private void dismissDialog() {

        try {
            if (mDialog != null && mDialog.isShowing()) {
                mDialog.dismiss();
            }
        } catch (Exception ignored) {
        } finally {
            mDialog = null;
        }
    }

    public void onDialogLoadingCancel() {
    }

}
